
from tkinter import *
from tkinter import ttk
import paramiko as po #python的ssh模块
import telnetlib as tb #python的telnet模块
import time



def add_host():
	def s_cont(host,port,user,passwd):#连接远程主机
		try:
			s = po.SSHClient()	#初始化ssh
			s.set_missing_host_key_policy(po.AutoAddPolicy())#将主机添加到know_hosts文件中
			s.connect(host,port,user,passwd)#连接主机
		except:
			s.close()#关闭连接
			return False
		else:
			s.close()#关闭连接
			return True

	def t_cont(host,port,user,passwd):
		print('')

	def test_cent():
		ur = user.get()
		pd = passwd.get()
		ip = h_ip.get()
		pt = port.get()
		var = c_cent()
		print('{}\t{}\t{}\t{}'.format(ip,pt,ur,pd))
		if var == 'ssh':
			print(s_cont(ip,pt,ur,pd))
		elif var == 'telnet':
			c_cont(ur,pd,pt,ip)

	def add_de():
		ur = user.get()
		pd = passwd.get()
		ip = h_ip.get()
		pt = port.get()
		sp = systp.get()

	def c_cent():
		cts = cent.get()
		if cts == 1:
			return 'ssh'
		elif cts == 2:
			return 'telnet'
	def funn():
		pass
	msg = ''
	rw = 320
	rh = 300
	host = tkinter.Toplevel()
	host.title('添加新主机')
	sw = host.winfo_screenwidth()#获取屏幕宽度
	sh = host.winfo_screenheight()#获取屏幕高度
	x = (sw-rw)/2#获取减去程序所占宽度后屏幕一半的宽度
	y = (sh-rh)/2#获取减去程序所占高度后屏幕一半的高度
	host.geometry('%dx%d+%d+%d'%(rw,rh,x,y))#设置窗口大小和显示位置
	host.resizable(0,0)#固定窗口大小

	user = StringVar()#获取账号
	passwd = StringVar()#获取密码
	port = StringVar()#获取端口
	systp = StringVar()#获取系统类型
	h_ip = StringVar()#获取主机IP
	cent = IntVar()#获取链接方式

	#Label(host,text='').grid(row=0,column=0)
	Label(host,text='请输入主机账号:',width=15).grid(row=1,column=0)
	Entry(host,width=25,bd=2,textvariable=user,relief=GROOVE).grid(row=1,column=1,pady=5)
	Label(host,text='请输入主机密码:',width=15).grid(row=2,column=0)
	Entry(host,width=25,bd=2,textvariable=passwd,relief=GROOVE,show='').grid(row=2,column=1,pady=5)
	Label(host,text='请选择操作系统:',width=15).grid(row=3,column=0)
	dd=ttk.Combobox(host,width=22,state='readonly',textvariable=systp)
	dd['values']=('------选择操作系统------','Ubuntu','Centos','Redhat','Debian','Windows')
	dd.current(0)
	dd.grid(row=3,column=1)
	Label(host,text='请输入远程端口:',width=15).grid(row=4,column=0)
	Entry(host,width=25,bd=2,textvariable=port,relief=GROOVE).grid(row=4,column=1,pady=5)
	Label(host,text='请输入主机IP:',width=15).grid(row=5,column=0,sticky=E)
	Entry(host,width=25,bd=2,textvariable=h_ip,relief=GROOVE).grid(row=5,column=1,pady=5)
	Label(host,text='请选择连接方式:',width=15).grid(row=6,column=0)
	Radiobutton(host,text='SSH',variable=cent,value=1,padx=15,pady=5).grid(row=6,column=1,sticky=W)
	Radiobutton(host,text='Telnet',variable=cent,value=2,padx=15,pady=5).grid(row=6,column=1,sticky=E)
	Label(host,text=msg,fg='green').grid(row=7,columnspan=2,pady=5)
	Button(host,text='测试连接',width=10,bg='red',command=test_cent).grid(row=8,columnspan=2,padx=55,sticky=W)
	Button(host,text='确定添加',width=10,bg='green',command=add_de).grid(row=8,columnspan=2,padx=35,sticky=E)
	

	host.mainloop()
def del_host():
	pass
def ur_info():
	pass
def implement():
	var = fn.get()
	for index,i in enumerate(lset):
		if i == var:
			break
	print(index,var)

def main_interface():
	rw = 1200
	rh = 800
	root = Tk()
	#root.geometry('rwxrh')
	root.title('98k创意运维管理系统V 1.0.0')
	sw = root.winfo_screenwidth()#获取屏幕宽度
	sh = root.winfo_screenheight()#获取屏幕高度
	x = (sw-rw)/2#获取减去程序所占宽度后屏幕一半的宽度
	y = (sh-rh)/2#获取减去程序所占高度后屏幕一半的高度
	root.geometry('%dx%d+%d+%d'%(rw,rh,x,y))#设置窗口大小和显示位置
	root.resizable(0,0)#固定窗口大小
	global comd
	global lset
	global fn
	lset = ('------下拉选择功能------','自定义脚本','自定义命令','备份(压缩)/还原(解压缩)','任务计划',\
	'部署LAMP环境','部署宝塔面板','部署Java JDK环境','部署Python3环境','部署PHP环境','安装MySQL数据库',\
	'安装Mongodb数据库','安装Redis数据库','安装Oracle数据库','安装Git工具','安装Docker容器','安装Android SDK')
	frame = Frame(root,width=1200,height=100,bg='red')
	Button(frame,text='添加主机',bg='green',command=add_host).grid(row=0,column=0,padx=20,sticky=W)
	Button(frame,text='删除主机',bg='red',command=del_host).grid(row=0,column=0,padx=100,sticky=W)
	Label(frame,text='功能选择:').grid(row=0,column=0,padx=200,sticky=W)
	fn=ttk.Combobox(frame,state='readonly',justify='center')
	fn['values']=lset#下拉框显示的内容
	fn.current(0)#下拉框默认显示的内容
	fn.grid(row=0,column=0,padx=270,sticky=W)
	
	Button(frame,text='确定',bg='green',command=implement).grid(row=0,column=0,padx=430,sticky=W)
	
	frame.grid(row=0,column=0,sticky=W)
	#Label(root,text='').grid(row=0,column=1)
	#Label(root,text='用户信息',width=15,bg='yellow').grid(row=0,column=2,sticky=E)
	fe=Frame(width=1200,height=600,bg='gray').grid(row=2,column=0)
	Button(fe,text='测试').grid()
	
	
	root.mainloop()
	

if __name__ == '__main__':
	main_interface()
